<?php
require_once '../../include/functions.php';
//set session save path
session_save_path("../../".$session_path);
// Initialize a session. 
session_start();
$count++;
$start = time();
$sessionId = session_id();
//Connect To Database
dbConnect();

//Get this week
if(($CurrWeek == "") || ($CurrWeek == NULL))
{
	$CurrWeek = getWeek("NFL");
	$CurrWeek--;
}
//if ($CurrWeek > 17)
//	$CurrWeek = 17;
//Write SQL Query to extract specified data
$SQL = "SELECT NFLSurvivalPicks.*, Users.ID, Users.username FROM NFLSurvivalPicks, Users WHERE Users.ID = NFLSurvivalPicks.UserID AND NFLSurvivalPicks.Week <= $CurrWeek ORDER BY NFLSurvivalPicks.Week ASC, NFLSurvivalPicks.UserID ASC";
//Open Record Set using SQL Query
$result = mysql_query($SQL) or die("Cannot query the database to get survivors.<br/>");
if (mysql_affected_rows() <= 0)
{
	echo "<h3>There are no users in the survival pool.</h3>";
	exit;
}
$firstWeek = 22;
$UsersRemaining[] = 0;
do{
	if (($myrow["username"] != Null) || ($myrow["username"] != ""))
	{
		if ($firstWeek > $myrow["Week"]) {$firstWeek = $myrow["Week"];}
		$users[$myrow["ID"]] = $myrow["username"];
		$picks[$myrow["ID"]][$myrow["Week"]] = $myrow["Pick"];
		$UsersRemaining[$myrow["Week"]]++;
	}
}while($myrow = mysql_fetch_array($result));

$eliminated = array_fill($firstWeek,$CurrWeek-$firstWeek, 0);
//Sort users by number of weeks in survival pool.
foreach ($users as $key => $value)
	$NumWeeks[$key] = count($picks[$key]);
arsort($NumWeeks);
// Start Display
?>
<p align="center">Hover over the <img src="../images/skull.jpg" style="vertical-align: middle" title="Eliminated"/> image to see which team the user picked that sealed his/her fate.</p>
<table cellpadding="5" cellspacing="0">
	<tr>
		<td align="center" class="redbg" height="1"><span class="whitelink"><?= date("Y"); ?> NFL Survivor Standings</span></td>
	</tr>
	<tr>
		<td class="redbg">
			<table cellspacing="0" cellpadding="3" border="1" align="center" style="border-collapse: collapse">
				<tr bgcolor="#FFFFFF">
					<TH width="100" align="left" valign="bottom" rowspan="2" style="border-left:  1px solid #fff; border-top: 1px solid #fff;">UserName</TH>
					<TH colspan="<?=$CurrWeek; ?>" align="center" style=" border-top: 1px solid #fff; border-left:  1px solid #fff; border-right: 1px solid #fff;">Week</TH>
				</tr>
				<tr bgcolor="#FFFFFF">
				<?
				for($i = $firstWeek; $i <= $CurrWeek; $i++)
					echo "<td width=\"40\" align=\"center\"><a href=\"$PHP_SELF?view='survival'&CurrWeek=$i\" class=\"permLink\">$i</a></td>\n";
				echo "</tr>";
				foreach ($NumWeeks as $key => $val)
				{
					if ($userID && ($userID == $key))
						echo "<tr bgcolor=\"#ffff33\" height=\"30\">";
					else
						echo "<tr bgcolor=\"#FFFFFF\" height=\"30\">";
					
					echo "<td>".makeUserLink($key)."</td>\n";
					for ($j = $firstWeek; $j <= $CurrWeek; $j++)
					{
						if ($picks[$key][$j] != "")
						{
							if (teamWon($picks[$key][$j], $j, "NFL"))
							{
								echo "<td align=\"center\"><img src=\"http://football.joshhughes.com/NFL/images/small/".$picks[$key][$j].".gif\" title=\"".$picks[$key][$j]."\"/></td>\n";
							}
							else	//The team the user picked lost
							{		//they are now eliminated.
								echo "<td align=\"center\"><img src=\"http://football.joshhughes.com/images/skull.jpg\" title=\"".$picks[$key][$j]."\"/></td>\n";
								$UsersRemaining[$j]--;	//Decrement users remaining for this week
								$eliminated[$j]++;		//increment users eliminated this week
							}
						}
						else
						{
							echo "<td align=\"center\"><img src=\"http://football.joshhughes.com/images/red-x.gif\" title=\"OUT\"/></td>\n";
						}
					}
					echo "</tr>";
				}
				?>
				<!-- ELIMINATED ROW -->
				<tr bgcolor="#FFFFFF">
					<th width="100" align="left" style="border-top: 3px solid #cb0000;">Eliminated:</th>
					<?
					for ($i = $firstWeek; $i <= $CurrWeek; $i++)//foreach ($eliminated as $key => $value)
					{
						if($eliminated[$i] > 0)
							echo "<th align=\"center\" style=\"border-top: 3px solid #cb0000;color: #cb0000\">$eliminated[$i]</th>\n";
						else
							echo "<td align=\"center\" style=\"border-top: 3px solid #cb0000;\">$eliminated[$i]</td>\n";
					}
					?>
				</tr>
				<!-- REMAINING ROW -->
				<tr bgcolor="#FFFFFF">
					<th width="100" align="left">Remaining:</th>
					<?
					for ($i = $firstWeek; $i <= $CurrWeek; $i++)//foreach ($UsersRemaining as $value)
					{
						echo "<th>".$UsersRemaining[$i]."</th>\n";
					}
					?>
				</tr>
			</table>
		</td>
	</tr>
</table>
<?
//end
?>